<template>
  <div class="container">
    <!-- 面包屑导航 -->
    <div class="breadcrumb">
         <navbar></navbar>
    </div>
    <!-- 面包屑导航 -->
    <div class="Box">
      <div class="table_tab">
        <div class="inquiry">
          <div class="headerbox">
            <div class="header">
              <div class="title_left"></div>
              标签回收站
            </div>
            <div class="headerbox_right">
              <!-- <el-button size="mini" type="primary" @click="refresh()"
                >刷新</el-button
              > -->
              <!-- <el-button size="mini" @click="back()">返回</el-button> -->
            </div>
          </div>
          <div class="headerTitle">数据筛选</div>
          <el-form
            :inline="true"
            ref="queryForm"
            :model="queryForm"
            label-width=""
            size="mini"
            class="search-form"
          >
            <el-form-item label="标签">
              <el-input
                placeholder="请输入标签"
                clearable
                :maxlength="20"
                v-model.trim="queryForm.name"
              ></el-input>
            </el-form-item>
            <el-form-item label="删除人">
               <el-select
                  v-model="queryForm.updatedBy"
                  filterable
                  clearable
                  placeholder="请选择删除人"
                  :default-first-option="true"
                >
                  <el-option
                    v-for="items in customer"
                    :key="items.id"
                    :label="items.reallyName"
                    :value="items.id"
                  ></el-option>
                </el-select>
            </el-form-item>

           <el-form-item label="删除时间" prop="createdTime">
            <el-date-picker
                v-model="deleteData"
                type="daterange"
                size="small"
                unlink-panels
                @change="onChangeTime"
                format="yyyy-MM-dd"
                value-format="yyyy-MM-dd HH:mm:ss"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                :default-time="['00:00:00', '23:59:59']">
            </el-date-picker>
          </el-form-item>
            <el-form-item>
              <el-button
                @click="searchEvent()"
                plain
                v-on:keyup.enter="searchEvent"
                type="primary"
                >查询</el-button
              >
              <el-button plain @click="reset()" type="primary">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
        <div class="inquiryTab">
          <div class="inquiryTab_header">
             <el-button plain v-for="(item,index) in dictionaries.customer_tag_type" :key="index"
                  class="tabBtn"
                  :type="currentStatus == item.dictId ? 'primary' : 'info'"
                  @click="onChangeList(item.dictId)"
              >{{item.dictName}}</el-button>
          </div>
          <div class="table">
            <el-table
             v-loading="loading"
              row-key="id"
              lazy
              :tree-props="{ children: 'tagList', hasChildren: 'hasChildren' }"
              :data="currentStatus==1? tableData.programmingTags : tableData.customTags"
              :header-cell-style="{
                background: '#f8f8f8',
                textAlign: 'left',
              }"
              style="width: 100%"
              :toggleRowExpansion="{ expanded: true }"
              ref="theTable"
            >
              <el-table-column
                label="全部标签"
                prop="categoryName"
                :show-overflow-tooltip="true"
              >
              <template slot-scope="props">
                    <span>{{ props.row.name }}</span>
                    <span v-if="props.row.tagList">{{ '(' + props.row.tagList.length + ')' }}</span>
                </template>
              </el-table-column>
              <el-table-column  label="删除人" prop="status">
                <template slot-scope="props">
                    {{ props.row.updatedByLabel }}
                </template>
              </el-table-column>
              <el-table-column
                label="删除时间"
                prop="updatedTime"
                label-class-name="updatedTime"
              >
                <template slot-scope="props">
                  <div style="text-align: center">
                    {{ props.row.updatedTime }}
                  </div>
                </template>
              </el-table-column>
              <el-table-column
                label="操作"
                align="center"
                label-class-name="updatedTime"
              >
                <template slot-scope="props">
                  <div>
                    <p
                      class="btnp"
                      @click="restore(props.row)"
                      v-if="props.row.status!=1&&parArray.indexOf('recover') != -1"
                    >
                      恢复
                    </p>
                    <p
                      class="btnp" style="color:red;"
                      @click="thoroughlyDelete(props.row)"
                      v-if="parArray.indexOf('deleteThorough') != -1&&props.row.status!=1"
                    >
                      彻底删除
                    </p>
                  </div>
                </template>
              </el-table-column>
            </el-table>
          </div>
        </div>
      </div>
    </div>
    <!-- 分页start -->
    <div class="page" v-if="tableData.totalNum > 0">
      <div class="block">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNo"
          :page-sizes="[10, 20, 50, 100, 200]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="tableData.totalNum"
          prev-text="上一页"
          next-text="下一页"
        >
        </el-pagination>
      </div>
    </div>
    <!-- 分页end -->
     <!--                                弹框                                       -->
      <el-dialog
        :visible.sync="dialog.visible"
        width="520px"
        :before-close="handledeleteClose"
        append-to-body
        class="deletegroup"
      >
        <div
          slot="title"
          class="header-title"
          style="display: flex; align-items: center"
        >
          <span :class="dialog.type=='delete'?'el-iconStyle':'el-iconStyleTwo'"><i class="el-icon-warning"></i></span>
          <span style="padding-left: 12px; font-size: 22px">{{dialog.title}}</span>
        </div>
        <span style="font-size: 16px; color: #333333"
          >是否{{labelState + labelName}}标签？</span
        >
        <span slot="footer" class="dialog-footer">
          <el-button
            @click="dialog.visible = false"
            style="
              width: 120px;
              height: 36px;
              border: 1px solid #d9d9d9;
              opacity: 1;
              line-height: 2px;
              border-radius: 4px;
            "
            >取 消</el-button
          >
          <el-button
            type="primary"
            @click="deleteGroup"
            style="
              width: 120px;
              height: 36px;
              line-height: 2px;
              opacity: 1;
              border-radius: 4px;
            "
            >确 定</el-button
          >
        </span>
      </el-dialog>
  </div>

</template>

<script>
import navbar from '@/components/navbar.vue'
import common from './mixins/common'

export default {
  mixins: [common],
  components: {
    navbar
  },
  data () {
    return {
      customer: [],
      loading: false,
      recoverRow: {}, // 恢复单行数据
      deleteRow: {}, // 删除的单行数据
      deleteData: [],
      labelState: '', // 删除或者恢复文字
      labelName: '', // 标签名称
      dialog: { visible: false, title: '', width: '', type: '' },
      currentStatus: 1, // 当前列表状态

      goodsNumber: '',
      date: '',
      rowId: '', // 列表行ID
      rowList: [],
      selectedOptions: [],
      position: [
        {
          label: '正常',
          value: 'A'
        },
        {
          label: '停用',
          value: 'S'
        },
        {
          label: '删除',
          value: 'X'
        }
      ],
      options: [],
      btnText: '',
      queryForm: {
        name: '',
        updatedBy: '',
        startTime: '',
        endTime: ''
      },
      // 列表树形控件配置参数
      defaultProps: {
        children: 'children',
        categoryName: 'categoryName'
      },
      // 新增下级
      ruleForm: {
        categoryNo: '',
        categoryName: '',
        parentId: '',
        status: 'A',
        value: 0
      },
      cascaderValue: '',
      ruleFormRules: {
        categoryNo: [
          { required: false, message: '请输入编号', trigger: 'blur' },
          { min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' }
        ],
        categoryName: [
          { required: true, message: '请输入分类名称', trigger: 'change' },
          { min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' }
        ],
        status: [
          { required: true, message: '请选择状态', trigger: 'change' }
        ]
      },
      // 新增分类
      form: {
        categoryNo: '',
        categoryName: '',
        parentId: '',
        status: 'A'
      },
      rules: {
        categoryNo: [
          { required: false, message: '请输入编号', trigger: 'blur' },
          { min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' }
        ],
        categoryName: [
          { required: true, message: '请输入分类名称', trigger: 'change' },
          { min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' }
        ],
        status: [
          { required: true, message: '请选择状态', trigger: 'change' }
        ]
      },
      forbidden: false, // 是否禁用
      pageNo: 1,
      pageSize: 10,
      tableData: [],
      tree: [],
      orgCode: '',
      limits: '',
      parArray: [],
      height: '500px',
      positionList: [
        {
          name: '启用',
          value: 'A'
        },
        {
          name: '禁用',
          value: 'S'
        }
      ], // 职位列表数据
      expandRow: []
    }
  },
  mounted () {
    // const self = this
    // const ph = document.documentElement.clientHeight
    // self.height = ph - 350 + 'px'
    // window.onresize = function () {
    //   const ph = document.documentElement.clientHeight
    //   self.height = ph - 350 + 'px'
    // }
  },
  created () {
    this.getDataList()
    this.getDeleteList()
    // 权限设定判断
    this.limits = JSON.parse(sessionStorage.getItem('limit'))
    const params = this.limits.buttonTreeDto.filter(
      (item) => item.menuCode === 'LableRecycleBin'// 菜单管理里面的菜单编码
    )
    params[0].buttonList.forEach((item) => {
      this.parArray.push(item.code)
    })
    console.log(this.parArray)
    // 权限设定判断
    document.onkeydown = (e) => {
      const key = window.event.keyCode
      if (key == 13) {
        this.searchEvent()
      }
    }
  },
  beforeRouteLeave (to, form, next) {
    document.onkeydown = null
    next()
  },

  methods: {

    getDeleteList () {
      // 客户下拉框数据
      this.axios.get(
        '/sys/v1/user/employee/by/sales/authority?isQueryDel=1&salesAuthorityId=' +
          JSON.parse(sessionStorage.getItem('salesAuthorityId').split(',')),
        {
          headers: {
            isLoading: false
          }
        }
      )
        .then((res) => {
          if (res.code == 0) {
            this.customer = res.data
          } else {
            this.$message.error(res.message)
          }
        })
    },
    getDataList () {
      this.loading = true
      const data = {
        status: 3,
        name: this.queryForm.name,
        updatedBy: this.queryForm.updatedBy,
        startTime: this.queryForm.startTime,
        endTime: this.queryForm.endTime
      }
      this.$api.tagRecycleList(data).then(res => {
        if (res.code == 0) {
          res.data.customTags.forEach((item) => {
            item.stair = true
          })
          res.data.programmingTags.forEach((it) => {
            it.stair = true
          })
          this.tableData = res.data
          this.loading = false
        }
        console.log(this.tableData, 'this.tableData')
      }).catch(err => { console.log(err) })
    },
    // 删除弹框确定
    deleteGroup () {
      if (this.dialog.type == 'restore') { // 恢复
        let baseUrl
        if (this.recoverRow.stair) {
          baseUrl = this.$api.tagRecycleRecoveryClassifyGroup
        } else {
          baseUrl = this.$api.tagRecycleRecoveryTag
        }
        const params = {
          id: this.recoverRow.id
        }
        baseUrl(params).then(res => {
          if (res.code == 0) {
            this.$message.success('恢复成功！')
            this.dialog.visible = false
            this.getDataList()
          }
        }).catch(err => { console.log(err) })
      } else { // 删除
        let baseUrl
        if (this.deleteRow.stair) {
          baseUrl = this.$api.tagRecycleDeleteClassifyGroup // 标签分类
        } else {
          baseUrl = this.$api.tagRecycleDeleteTag // 标签
        }
        const params = {
          id: this.deleteRow.id
        }
        baseUrl(params).then(res => {
          if (res.code == 0) {
            this.$message.success('删除成功！')
            this.dialog.visible = false
            this.getDataList()
          }
        }).catch(err => { console.log(err) })
      }
    },
    // 恢复
    restore (res) {
      console.log(res, 'res')
      this.dialog.visible = true
      this.dialog.title = '确认提示'
      this.dialog.type = 'restore'
      this.labelName = res.name
      this.labelState = '恢复'
      this.recoverRow = res
    },
    // 彻底删除
    thoroughlyDelete (row) {
      this.dialog.visible = true
      this.dialog.title = '删除提示'
      this.dialog.type = 'delete'
      this.labelName = row.name
      this.labelState = '删除'
      this.deleteRow = row
    },
    handledeleteClose () {
      this.dialog.visible = false
    },
    // 切换当前列表
    onChangeList (state) {
      this.page = { pageNo: 1, pageSize: 10, totalNum: 0, currentPage: 1 }
      this.currentStatus = Number(state)
      // this.$set(this, 'currentStatus', state)
      this.getDataList()
      console.log(this.currentStatus)
    },
    // 时间选择
    onChangeTime (time) {
      if (time) {
        this.queryForm.startTime = time[0]
        this.queryForm.endTime = time[1]
      } else {
        this.queryForm.startTime = ''
        this.queryForm.endTime = ''
      }
    },
    selectState (e) {
      this.queryForm.status = e
      this.getStaffList()
    },
    handleNodeClick (e) {
      console.log(e, '树形控件')
    },
    getStaffList () {
      const params = {
        pageNo: this.pageNo,
        pageSize: this.pageSize,
        categoryNo: this.queryForm.categoryNo,
        categoryName: this.queryForm.categoryName,
        parentId: 0,
        status: this.queryForm.status
      }
      this.$api.categoryQuery(params).then((res) => {
        if (res.code == 0) {
          this.tableData = res.data
        }
        console.log(res)
      }).catch((err) => { console.log(err) })
    },
    // 刷新
    refresh () {
      this.queryForm = {}
      this.queryForm.orgId = ''
      this.getStaffList()
      this.$forceUpdate()
    },
    // 返回
    back () {
      this.$router.go(-1)
    },
    filterNode (value, data) {
      if (!value) return true
      return data.orgName.indexOf(value) !== -1
    },

    is_null (content) {
      if (content) {
        return false
      } else {
        return true
      }
    },
    // 分页
    handleCurrentChange (val) {
      this.pageNo = val
      this.getStaffList(this.orgCode)
    },
    handleSizeChange (val) {
      this.pageSize = val
      this.getStaffList(this.orgCode)
    },
    /** *是否正常下拉框的数据请求***/
    /** *重置搜索***/
    reset () {
      this.queryForm.name = ''
      this.queryForm.updatedBy = ''
      this.deleteData = []
      this.queryForm.startTime = ''
      this.queryForm.endTime = ''
      this.getDataList()
      if (this.currentStatus == 1) {
        this.forArr(this.tableData.programmingTags, false)// 全部展开树形表格
      } else {
        this.forArr(this.tableData.customTags, false)// 全部展开树形表格
      }
    },
    /** *搜索模拟数据***/
    searchEvent () {
      console.log(this.queryForm)
      // this.page.pageSize = 10
      // this.page.pageNo = 1
      this.getDataList()

      if (this.currentStatus == 1) {
        this.forArr(this.tableData.programmingTags, true)// 全部展开树形表格
      } else {
        this.forArr(this.tableData.customTags, true)// 全部展开树形表格
      }
    },
    /** *搜索模拟数据***/
    forArr (arr, isExpand) { // 展开树形表格
      arr.forEach(i => {
        this.$refs.theTable.toggleRowExpansion(i, isExpand)
        if (i.children) {
          this.forArr(i.children, isExpand)
        }
      })
    }

  }
}
</script>
<style scoped lang="scss">
.el-menu-vertical-demo {
  width: 200px;
  overflow-y: scroll;
  height: 700px;
}
.el-menu-input {
  padding: 0 15px;
  background: #fff;
  /deep/ .el-input__inner {
    height: 30px;
    border: 1px solid #eee;
    margin: 10px 0;
  }
}
.el-menu-vertical-demo::-webkit-scrollbar {
  width: 0;
  height: 0;
}
.btnp {
  display: inline-block;
  vertical-align: middle;
  padding: 0 10px;
  color: #0083ef;
}
.btnp:hover {
  color: #4168e7;
  cursor: pointer;
}
.Box {
  display: flex;
  justify-content: flex-start;
  width: 100%;

  .table_tab {
    width: 100%;
    margin-bottom: 50px;
  }
}
.table {
  border-radius: 5px;
  width: 100%;
}
/deep/.distpicker-address-wrapper select {
  padding: 0rem 0.75rem;
  height: 30px;
  line-height: 30px;
  font-size: 15px;
}

/deep/ .expanded {
  background: #f5f7fa;
  color: #000;
}

/deep/.el-table__expanded-cell[class*="cell"] {
  padding: 0px 50px;
}
/deep/.el-table td,
.el-table th {
  text-align: left;
}
.header {
  display: flex;
  align-items: center;
  font-size: 18px;
  font-family: PingFang SC;
  font-weight: bold;
  color: #333333;
  padding-bottom: 10px;
}

.inquiry {
  background: #fff;
  padding: 10px 20px 10px 20px;
  border-radius: 5px;
  .el-form {
    margin-top: 10px;
  }
}
.inquiryTab {
  background: #fff;
  padding: 20px;
  border-radius: 5px;
  .el-form {
    margin-top: 10px;
  }
  margin-top: 20px;
  .inquiryTab_header {
    display: flex;
    margin-bottom: 20px;
    /deep/ .el-button {
      height: 40px;
    }
  }
}

/deep/.el-table__expand-icon > .el-icon {
  color: transparent;
}

.table {
  margin-top: 10px;
  border: 1px solid #e8eaec;
  border-radius: 0;
  /deep/ .el-input--suffix .el-input__inner {
    background: #409eff;
    color: #fff;
    width: 104px;
    box-shadow: none;
  }
  /deep/ .el-button {
    padding: 10px 13px;
  }
}

.page {
  text-align: right;
  width: 100%;
  background: #f5f5f5;
  padding-right: 30px;
  position: fixed;
  bottom: 0px;
  right: 0px;
  z-index: 5;

  span {
    font-size: 14px;
    color: #666;
    display: inline-block;
    margin-left: 10px;
  }
  .el-pagination {
    display: inline-block;
    vertical-align: middle;
  }
}
.demo-table-expand {
  font-size: 0;
}
.demo-table-expand label {
  width: 200px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
  text-align: left;
}
/deep/.el-table__row:hover {
  color: rgb(45, 125, 223);
}
/deep/.el-dialog__header {
  padding: 20px 20px 20px 20px;
  border-bottom: 1px solid #d9d9d9;
  font-family: PingFang SC;
  font-weight: bold;
  line-height: 25px;
  color: #333333;
}
/deep/.el-dialog {
  border-radius: 10px;
}
/deep/.el-dialog__headerbtn {
  top: 15px;
}
.headerbox {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 10px;
  border-bottom: 1px solid #ededed;
  padding-bottom: 10px;
}
.title_left {
  width: 4px;
  height: 16px;
  background: #4c81eb;
  opacity: 1;
  margin-right: 10px;
}
.tabTitle {
  font-size: 18px;
  font-family: PingFang SC;
  font-weight: 400;
  line-height: 25px;
  color: #333333;
  opacity: 1;
  margin-top: 10px;
  margin-bottom: 20px;
}
.headerTitle {
  font-size: 18px;
  font-family: PingFang SC;
  font-weight: 400;
  color: #333333;
  opacity: 1;
  margin-top: 20px;
  margin-bottom: 15px;
}
/deep/.el-form-item__label {
  font-size: 14px;
  font-family: PingFang SC;
  font-weight: 400;
  color: #333333;
}
.red {
  color: #ff0000;
}
.green {
  color: #67c23a;
}

/deep/.el-input.is-disabled .el-input__inner {
  background-color: #fff;
  font-family: PingFang SC;
  font-weight: 400;
  color: #333333;
}
/deep/ .el-dialog__footer {
  border-top: 1px solid #d9d9d9;
  margin-top: -20px;
  padding-top: 20px;
}
/deep/.el-table thead {
  font-size: 16px;
  font-family: PingFang SC;
  font-weight: bold;
  line-height: 22px;
  color: #333333;
  opacity: 1;
}
/deep/.el-table th > .cell {
  text-align: left;
}
/deep/.updatedTime {
  text-align: center !important;
}
.btnClass{
  margin-top: 20px;
  display: flex;
  justify-content: right;
}
.tabBtn{
  width: 120px !important;
  height: 36px !important;
  line-height: 5px;
  border: none;
}
.deletegroup {
  /deep/.el-dialog .el-dialog__header {
    background-color: #fff;
  }
  /deep/ .el-dialog__header {
    border: none;
  }
  /deep/.el-dialog__body {
    padding: 0 40px 10px 60px;
  }
  /deep/ .el-dialog__footer {
  border-top:none !important;
  margin-top: 10px;

}
.el-iconStyle{
  font-size: 28px;
  color: red
}
.el-iconStyleTwo{
  font-size: 28px;
  color:#EDB840
}
}

.search-form {
  display: flex;
  flex-wrap: wrap;
  .el-form-item {
    width: 100%;
    max-width: 303px;
    min-width: 299px;
    /deep/.el-form-item__label{
      width: 70px;
    }
    /deep/.el-form-item__content {
      width: calc(100% - 70px);
      .el-select, .el-date-editor, .el-cascader {
        width: 100%;
      }
    }
  }
}
</style>
